<#
规则/文档索引生成（原生 PowerShell）
用途：提取 rules/ 与 docs/ 文件清单，生成 .tmp/rules_extracted.md。
用法：
  pwsh -File scripts/windows/gen-mapping-doc.ps1
#>
param()

$WindowsDir = Split-Path -Parent $PSCommandPath
$ScriptsDir = Split-Path -Parent $WindowsDir
$RepoDir = Split-Path -Parent $ScriptsDir

$outDir = Join-Path $RepoDir '.tmp'
if (!(Test-Path $outDir)) { $outDir = Join-Path $RepoDir '.tmp' }
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$out = Join-Path $outDir 'rules_extracted.md'

$rulesDir = Join-Path $RepoDir 'rules'
if (!(Test-Path $rulesDir)) { $rulesDir = Join-Path $RepoDir 'rules' }
$docsDir = Join-Path $RepoDir 'docs'
if (!(Test-Path $docsDir)) { $docsDir = Join-Path $RepoDir 'docs' }

$lines = @('# 规则与文档索引（自动生成）', "", '## rules/')
Get-ChildItem -Path $rulesDir -Recurse -File -Include *.md | ForEach-Object { $rel = $_.FullName.Substring($RepoDir.Length).TrimStart('\','/'); $lines += "- $rel" }
$lines += ''
$lines += '## docs/'
Get-ChildItem -Path $docsDir -Recurse -File -Include *.md | ForEach-Object { $rel = $_.FullName.Substring($RepoDir.Length).TrimStart('\','/'); $lines += "- $rel" }

Set-Content -Path $out -Value ($lines -join "`n")
Write-Host "已生成：$out"
exit 0
